package com.ruoyi.web.controller.front;

import com.ruoyi.api.domain.GuidEntity;
import com.ruoyi.api.domain.GuidRegionEntity;
import com.ruoyi.api.service.GuidDayScenicService;
import com.ruoyi.api.service.GuidService;
import com.ruoyi.api.service.UserGuidService;
import com.ruoyi.api.vo.GuidVo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/front/guids")
public class GuidsController  extends BaseController {

    @Autowired
    private GuidService guidService;

    @Autowired
    private UserGuidService userGuidService;

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 获取所有guid
     */
    @PreAuthorize("@ss.hasPermi('system:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(GuidEntity user,
                              @RequestParam(value = "pageNum", defaultValue = "0") int pageNum,
                              @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) throws IllegalAccessException {



        Page<GuidEntity> resultPage = guidService.selectList(GuidEntity.class,user,pageNum-1,pageSize,mongoTemplate);
        // 调用服务层方法，获取分页数据
        List<GuidEntity> list = guidService.setCate(resultPage.getContent());
        // 转换分页结果为 TableDataInfo

        return getDataTable(list, resultPage.getTotalElements());
    }

    /**
     * 根据用户编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:user:query')")
    @GetMapping(value = { "/", "/{id}" })
    public AjaxResult getInfo(@PathVariable(value = "id", required = false) String id)
    {
        GuidEntity guidEntity = guidService.getInfoById(GuidEntity.class,id,mongoTemplate);
        GuidVo guidVo = guidService.setPlans(guidEntity);
        return AjaxResult.success(guidVo);
    }

    /**
     * 新增
     */
    @PreAuthorize("@ss.hasPermi('system:user:add')")
    @Log(title = "新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GuidEntity user)
    {
        user.setCreateBy(getUsername());
        if (null == user.getId()) {
            user.setAttribute(getUsername());
        }

        boolean res =  guidService.update(GuidEntity.class,user,mongoTemplate);
        //更新分类
        if(null!=user.getCategories() && res && user.getCategories().split(",").length>0){

           guidService.updateCate(user.getId(),user.getCategories().split(","));

        }

        if(null!=user.getPlans()){
            userGuidService.savePlans(user);
        }
        return AjaxResult.success();

    }


    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('system:user:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return AjaxResult.success(guidService.deleteByIds(GuidEntity.class,ids,mongoTemplate));
    }


}
